<?

class Mojito_Form
{
	private static $_Loader=null;
	/**
	 * _defaultOptions
	 * 
	 * (default value: array('path'=>null,'prefix'=>''))
	 * 
	 * @var array
	 * @access private
	 */
	private static $_defaultOptions=array('path'=>null,'prefix'=>'');
	
	/**
	 * _options
	 * 
	 * (default value: array())
	 * 
	 * @var array
	 * @access private
	 */
	private static $_options=array();
	
	/**
	 * formInstances
	 * internally maintains instances of forms accessed via Mojito_Form::get()
	 * (default value: array())
	 * 
	 * @var array
	 * @access private
	 */
	private static $formInstances=array();
	
	/**
	 * setOptions function.
	 * must be called before any called to get(). Bootstrap is a good place.
	 * @access public
	 * @static
	 * @param mixed array $options
	 * @return void
	 */
	public static function setOptions(array $options)
	{
		self::$_options = array_merge(self::$_defaultOptions,$options);
		if (empty(self::$_options['path'])) throw new Mojito_Form_Exception('path option must be a string pointing to folder containing form scripts.');
	}
	
	/**
	 * get function.
	 * factory pattern to access forms
	 * @access public
	 * @static
	 * @param mixed $form
	 * @return void
	 */
	public static function get($form)
	{
		$Loader=self::getLoader();
		$form=ucfirst($form);
		if (empty(self::$formInstances[$form]))
		{
			$class=$Loader->load($form);
			if ($class!==false)
			{
				$Form=new $class();
				self::$formInstances[$form] = $Form;
			}
		}
		return self::$formInstances[$form];
	}
	
	public static function getLoader()
	{
		if (!self::$_Loader instanceof Zend_Loader_PluginLoader) self::$_Loader=new Zend_Loader_PluginLoader(array(self::$_options['prefix']=>self::$_options['path']),'Mojito_Form');
		return self::$_Loader;
	}
	
	public static function getPrefix()
	{
		return self::$_options['prefix'];
	}
	
	public static function getPath()
	{
		return self::$_options['path'];
	}
}